package cn.tedu.boot04.controller;

import cn.tedu.boot04.entity.User;
import cn.tedu.boot04.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/reg")
    public String reg(User user) {
        System.out.println("user = " + user);
        //通过用户输入的用户名,查询出数据库中的用户信息
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return "用户名已存在!请<a href='/reg.html'>重新注册</a>";
        }
        mapper.insert(user);
        return "注册成功!<a href='/login.html'>登录</a>";
    }
    @RequestMapping("/login")
    public String login(User user){
        System.out.println("user = " + user);
        //通过用户输入的用户名查询数据库的密码
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            //判断用户输入的密码和查询到的密码是否一直
            if (u.getPassword().equals(user.getPassword())){
                return "登陆成功!";
            }
            return "密码错误,请<a href='/login.html'>重新登录</a>";
        }
        return "用户名不存在!请<a href='/login.html'>重新登录</a>";
    }
}
